#include "precomp.h"
#include "GoalMoveToPos.h"
#include "../Entity.h"
#include "../../Map/Grid.h"

namespace Tmpl {

GoalMoveToPos::GoalMoveToPos(Entity* owner, Vec2f target) : mTarget(target), Goal(owner)
{
}

void GoalMoveToPos::Activate()
{
	mStatus = Goal::GOALSTATUS_ACTIVE;
}
void GoalMoveToPos::Process(char threadId)
{
	// get data
	float maxSpeed = mOwner->GetMaxSpeed();
	Vec2f vDist = mTarget - mOwner->GetPos();
	float len = vDist.Length();

	// update entity
	if ( len > 0.0f ) mOwner->SetFacing(vDist / len);
	mOwner->SetSpeed(len);

	// return status
	if (len < maxSpeed + mOwner->GetSize()*0.5f)
	{
		mStatus = Goal::GOALSTATUS_COMPLETED;
	}
}
void GoalMoveToPos::Terminate()
{
	mOwner->SetSpeed(0.0f);
}

void GoalMoveToPos::Render() const
{
	if (mStatus == Goal::GOALSTATUS_ACTIVE)
	{
		Render::RenderLine2D(mOwner->GetPos(), mTarget, Vec3f(1,1,0));
	}
}

}